package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.quyeying.annotation.CacheLock;
import com.quyeying.annotation.CacheParam;
import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblAddressEntity;
import com.quyeying.entity.TblGiftReceiveEntity;
import com.quyeying.entity.TblOrderBaseEntity;
import com.quyeying.service.TblGiftReceiveService;
import com.quyeying.service.TblOrderBaseService;;
import com.quyeying.service.WxService;
import com.quyeying.utils.IdGeneratorSnowflake;
import com.quyeying.utils.TokenUtil;
import com.quyeying.utils.kdn.KdnUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* tbl_order_base 前端控制器
* </p>
* @author skx
* @since 2022-07-19
*/
@Slf4j
@RestController
@RequestMapping("orderBaseEntity")
public class TblOrderBaseController {

    @Resource
    TblOrderBaseService tblOrderBaseService;
    @Resource
    KdnUtil kdnUtil;
    @Resource
    WxService wxService;
    @Resource
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Resource
    TblGiftReceiveService giftReceiveService;

    /**
     * 查询快递信息
     */
    @GetMapping("/queryExpressInfo")
    public R queryExpressInfo(String shipperCode, String logisticCode){
        try {
            JsonNode resJson = kdnUtil.expressQuery(shipperCode, logisticCode);
            return R.success(resJson);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取用户订单列表信息
     */
    @GetMapping("/findListByPage")
    public R findListByPage(HttpServletRequest request, Integer current, Integer size, Integer orderStatus){
        try {
            return tblOrderBaseService.findListByPage(request, current, size, orderStatus);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 取消订单
     */
    @CacheLock(prefix = "cancelOrder")
    @PostMapping("/cancelOrder")
    public R cancelOrder(@RequestBody @CacheParam JsonNode jsonNode) {
        //取消类型 0 未付款取消 1未发货取消 2 已发货取消 3 已完成取消
        int cancelType = jsonNode.get("cancelType").intValue();
        //订单ID
        long orderId = jsonNode.get("orderId").longValue();
        return tblOrderBaseService.cancelOrder(orderId, cancelType);
    }

    /**
     * 确认收货
     */
    @PostMapping("/confirmOrder")
    public R confirmOrder(@RequestBody JsonNode jsonNode) {
        long orderId = jsonNode.get("orderId").longValue();
        tblOrderBaseService.confirmOrder(orderId);
        return R.success();
    }

    /**
     * 待支付订单付款
     */
    @GetMapping("pendingOrderPay")
    public R pendingOrderPay(Long orderId) {

        try {
            TblOrderBaseEntity orderBaseEntity = tblOrderBaseService.getById(orderId);
            orderBaseEntity.setOrderNo(String.valueOf(idGeneratorSnowflake.snowflakeId()));
            tblOrderBaseService.updateById(orderBaseEntity);
            Map<String, Object> map = wxService.miniJsapi(orderBaseEntity);
            map.put("orderId",orderId);
            return R.success(map);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * id查询
     **/
    @GetMapping("/findById")
    public R findById(Long id){
        try {
            return tblOrderBaseService.findById(id);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 撤销申请退款
     */
    @PostMapping("/revokeRefund")
    public R revokeRefund(@RequestBody JsonNode jsonNode) {
        long orderId = jsonNode.get("orderId").longValue();
        return tblOrderBaseService.revokeRefund(orderId);
    }

    /**
     * 查询订单状态数量
     **/
    @GetMapping("/findOrderTypeNum")
    public R findOrderTypeNum(HttpServletRequest request){
        try {
            return tblOrderBaseService.findOrderTypeNum(request);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 获取礼物订单未读数
     */
    @GetMapping("/getUnreadNum")
    public R getUnreadNum(HttpServletRequest request) {
        long userId = TokenUtil.getUserId(request);
        Integer unreadNum = tblOrderBaseService.getUnreadNum(userId);
        HashMap<Object, Object> map = new HashMap<>(1);
        map.put("unreadNum", unreadNum);
        return R.success(map);
    }

    /**
     * 获取领取礼物页面数据接口
     * status可领取状态 0正常 1已抢光 2已失效 3重复领取
     */
    @GetMapping("/getReceiveGiftData")
    @PassToken
    public R getReceiveGiftData(HttpServletRequest request, Long orderId){
        return tblOrderBaseService.getReceiveGiftData(request, orderId);
    }

    /**
     * 领取礼物
     */
    @PostMapping("/receiveGifts")
    public R receiveGifts(HttpServletRequest request, Long orderId, @RequestBody Map<String, Object> params){
        return tblOrderBaseService.receiveGifts(request, orderId, params);
    }

    /**
     *获取送我的订单列表
     */
    @GetMapping("/getReceiveGiftsList")
    public R getReceiveGiftsList(HttpServletRequest request, Integer current, Integer size) {
        return tblOrderBaseService.getReceiveGiftsList(request, current, size);
    }


    /**
     *获取礼物订单领取记录
     */
    @GetMapping("/getGiftCollectionList")
    public R getGiftCollectionList(Long id) {
        return tblOrderBaseService.getGiftCollectionList(id);
    }

    /**
     * 获取我的送礼订单列表
     */
    @GetMapping("/getGiveGiftsList")
    public R getGiveGiftsList(HttpServletRequest request, Integer current, Integer size) {
        return tblOrderBaseService.getGiveGiftsList(request, current, size);
    }

    /**
     * 获取我的送礼订单的子订单列表
     */
    @GetMapping("/getSonOrderList")
    public R getSonOrderList(Long orderId) {
        return tblOrderBaseService.getSonOrderList(orderId);
    }

    /**
     * 礼物领取留言
     */
    @PostMapping("/leaveMsg")
    public R leaveMsg(@RequestBody JsonNode jsonNode) {
        long giftId = jsonNode.get("giftId").asLong();
        String leaveMsg = jsonNode.get("leaveMsg").asText();
        TblGiftReceiveEntity giftReceiveEntity = new TblGiftReceiveEntity();
        giftReceiveEntity.setLeaveMsg(leaveMsg);
        QueryWrapper<TblGiftReceiveEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", giftId);
        giftReceiveService.update(giftReceiveEntity, wrapper);
        return R.success();
    }


    /**
     * 自购订单改礼品订单
     */
    @PostMapping("/orderUpdateState")
    public R orderUpdateState(HttpServletRequest request, @RequestBody Map<String, Object> params){
        return tblOrderBaseService.orderUpdateState(request, params);
    }


    /**
     * 待领取送礼订单填写收货地址
     */
    @PostMapping("/orderUpdateAddress")
    public R orderUpdateAddress(HttpServletRequest request, @RequestBody Map<String, Object> params){
        return tblOrderBaseService.orderUpdateAddress(request, params);
    }
}
